//
//  ViewController.m
//  CellFactoryModel
//
//  Created by 向祖华 on 16/4/22.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "ViewController.h"
#import "RootTableViewCell.h"
#import "OneCell.h"
#import "TwoCell.h"
#import "ThreeCell.h"
#import "CellFactory.h"
#import "DataModel.h"



@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray * allDataArr;//存储数据的数组
@property(nonatomic,strong)NSMutableArray * allHeightArr;//存储细胞高度的数组

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //处理数据
    [self getData];
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    //高度自适应
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 2;
    
    
    
    
}
#pragma mark -- 处理数据
-(void)getData{
    NSString * path = [[NSBundle mainBundle]pathForResource:@"DataList" ofType:@"plist"];
    NSDictionary * dataDic = [[NSDictionary alloc] initWithContentsOfFile:path];
    for (NSDictionary * dic in dataDic[@"dataArr"]) {
        DataModel * model = [[DataModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.allDataArr addObject:model];
    }
}
#pragma mark -- 实现tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allDataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RootTableViewCell * cell = nil;
    DataModel * model = self.allDataArr[indexPath.row];
    if ([model.count integerValue] == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"OneCell"];
        if (cell == nil) {
            cell = [CellFactory  creatCellWithClassName:@"OneCell" dataModel:model];
        }else{
        [[cell.contentView.subviews lastObject] removeFromSuperview];
        cell = [CellFactory creatCellWithClassName:@"OneCell" dataModel:model];
        }
        
    }else if([model.count integerValue]==2){
        cell = [tableView dequeueReusableCellWithIdentifier:@"TwoCell"];
        if (cell == nil) {
            cell = [CellFactory creatCellWithClassName:@"TwoCell" dataModel:model];
        }else{
            [[cell.contentView.subviews lastObject] removeFromSuperview];
            cell = [CellFactory creatCellWithClassName:@"TwoCell" dataModel:model];
        }
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeCell"];
        if (cell == nil) {
            cell = [CellFactory creatCellWithClassName:@"ThreeCell" dataModel:model];
        }else{
            [[cell.contentView.subviews lastObject] removeFromSuperview];
            cell = [CellFactory creatCellWithClassName:@"ThreeCell" dataModel:model];
        }
        
    }
    CGFloat cellHeigh = [cell cellHeight];
    [self.allHeightArr addObject:[NSNumber numberWithFloat:cellHeigh]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *number = self.allHeightArr[indexPath.row];
    return [number floatValue];

}






#pragma mark -- 懒加载
-(NSMutableArray *)allDataArr{
    if (!_allDataArr) {
        _allDataArr = [[NSMutableArray alloc] init];
    }
    return _allDataArr;
}
-(NSMutableArray *)allHeightArr{
    if (!_allHeightArr) {
        _allHeightArr = [[NSMutableArray alloc] init];
    }
    return _allHeightArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
